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,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,2 },
	{ 2,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,2 },
	{ 2,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,2 },
	{ 2,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,2 },
	{ 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2 },
	{ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 }
};

Level::Level() {
	canvas = 0;
	current_level.id = 0;
	num_levels = 0;

	for (u8 i = 0; i < MAX_LEVELS; i++) {
		levels[i].id = 0;
	}
}

Level::~Level() {
}

void Level::init(Canvas *the_canvas) {
	canvas = the_canvas;

	map.init(canvas, LEVEL_WIDTH, LEVEL_HEIGHT, TILE_WIDTH, TILE_HEIGHT);

	tiles[0].load_from_file(options.full_path("tiles/empty.tile"));
	tiles[1].load_from_file(options.full_path("tiles/movable_block.tile"));
	tiles[2].load_from_file(options.full_path("tiles/stationary_block.tile"));
	tiles[3].load_from_file(options.full_path("tiles/mouse.tile"));
	tiles[4].load_from_file(options.full_path("tiles/cat.tile"));
	tiles[5].load_from_file(options.full_path("tiles/yarn.tile"));
	tiles[6].load_from_file(options.full_path("tiles/trap.tile"));
	tiles[7].load_from_file(options.full_path("tiles/sinkhole.tile"));
	tiles[8].load_from_file(options.full_path("tiles/cheese.tile"));
	tiles[9].load_from_file(options.full_path("tiles/mouse_sinkhole.tile"));
	tiles[10].load_from_file(options.full_path("tiles/border_opening_vertical.tile"));
	tiles[11].load_from_file(options.full_path("tiles/border_opening_horizontal.tile"));
	tiles[12].load_from_file(options.full_path("tiles/cat_sitting.tile"));
	tiles[13].load_from_file(options.full_path("tiles/mouse_dead.tile"));
	tiles[14].load_from_file(options.full_path("tiles/yarn_dead.tile"));

	map.add_tile(&tiles[0], TILE_EMPTY);
	map.add_tile(&tiles[1], TILE_MOVABLE_BLOCK);
	map.add_tile(&tiles[2], TILE_STATIONARY_BLOCK);
	map.add_tile(&tiles[3], TILE_MOUSE);
	map.add_tile(&tiles[4], TILE_CAT);
	map.add_tile(&tiles[5], TILE_YARN);
	map.add_tile(&tiles[6], TILE_MOUSE_TRAP);
	map.add_tile(&tiles[7], TILE_SINK_HOLE);
	map.add_tile(&tiles[8], TILE_CHEESE);
	map.add_tile(&tiles[9], TILE_MOUSE_SINKHOLE);
	map.add_tile(&tiles[10], TILE_BORDER_OPENING_VERTICAL);
	map.add_tile(&tiles[11], TILE_BORDER_OPENING_HORIZONTAL);
	map.add_tile(&tiles[12], TILE_CAT_SITTING);
	map.add_tile(&tiles[13], TILE_MOUSE_DEAD);
	map.add_tile(&tiles[14], TILE_YARN_DEAD);

	load_levels_from_file(options.full_path("levels"));
}

void Level::load() {
	for (u8 i = 0; i < num_levels; i++) {
		if (levels[i].id == current_level.id) {
			current_level = levels[i];
			break;
		}
	}

	switch (current_level.type) {
		case LEVEL_SQUARE:
			switch (current_level.size) {
				case LEVEL_SMALL:
					load_map(LEVEL_SQUARE_SMALL);
					break;

				case LEVEL_MEDIUM:
					load_map(LEVEL_SQUARE_MEDIUM);
					break;

				case LEVEL_LARGE:
					load_map(LEVEL_SQUARE_LARGE);
					break;
			}
			break;

		case LEVEL_SCATTERED:
			load_map(LEVEL_EMPTY);
			break;

		case LEVEL_STATIONARY_CHECKERBOARD:
			switch (current_level.size) {
				case LEVEL_SMALL:
					load_map(LEVEL_STATIONARY_CHECKERBOARD_SMALL);
					break;

				case LEVEL_MEDIUM:
					load_map(LEVEL_STATIONARY_CHECKERBOARD_MEDIUM);
					break;

				case LEVEL_LARGE:
					load_map(LEVEL_STATIONARY_CHECKERBOARD_LARGE);
					break;
			}
			break;

		case LEVEL_MOVABLE_CHECKERBOARD:
			switch (current_level.size) {
				case LEVEL_SMALL:
					load_map(LEVEL_MOVABLE_CHECKERBOARD_SMALL);
					break;

				case LEVEL_MEDIUM:
					load_map(LEVEL_MOVABLE_CHECKERBOARD_MEDIUM);
					break;

				case LEVEL_LARGE:
					load_map(LEVEL_MOVABLE_CHECKERBOARD_LARGE);
					break;
			}
			break;
	}

	randomly_disperse_tile(current_level.movable_block_density, TILE_MOVABLE_BLOCK);
	randomly_disperse_tile(current_level.stationary_block_density, TILE_STATIONARY_BLOCK);
	randomly_disperse_tile(current_level.trap_density, TILE_MOUSE_TRAP);
	randomly_disperse_tile(current_level.sinkhole_density, TILE_SINK_HOLE);
}

bool Level::is_last() {
	LevelId last = 0;

	for (u8 i = 0; i < num_levels; i++) {
		if (levels[i].id > last) {
			last = levels[i].id;
		}
	}

	return (current_level.id == last);
}

void Level::next() {
	if (!is_last()) {
		for (u8 i = 0; i < num_levels; i++) {
			if (levels[i].id == current_level.id) {
				current_level = levels[i + 1];
				break;
			}
		}
	}
}

void Level::load_levels_from_file(const char *filename) {
	FILE *fh = fopen(filename, "r");

	if (!fh) {
		return;
	}

	num_levels = 0;
	LevelProperties level = DEFAULT_LEVEL_PROPERTIES;

	while (!feof(fh)) {
		char line[256];
		fgets(line, sizeof(line), fh);

		char *equals_sign_pos = strchr(line, '=');
		char *newline_pos = strchr(line, '\n');

		if (equals_sign_pos != NULL) {
			*equals_sign_pos = '\0';
			*newline_pos = '\0';

			char *property_name = line;
			char *value_name = (equals_sign_pos + 1);

			s32 int_value;

			if (!strcmp(property_name, "level")) {
				if (intval(value_name, &int_value)) {
					level.id = int_value;
				}
			} else if (!strcmp(property_name, "type")) {
				if (!strcmp(value_name, "square")) {
					level.type = LEVEL_SQUARE;
				} else if (!strcmp(value_name, "scattered")) {
					level.type = LEVEL_SCATTERED;
				} else if (!strcmp(value_name, "movable_checkerboard")) {
					level.type = LEVEL_MOVABLE_CHECKERBOARD;
				} else if (!strcmp(value_name, "stationary_checkerboard")) {
					level.type = LEVEL_STATIONARY_CHECKERBOARD;
				}
			} else if (!strcmp(property_name, "size")) {
				if (!strcmp(value_name, "small")) {
					level.size = LEVEL_SMALL;
				} else if (!strcmp(value_name, "medium")) {
					level.size = LEVEL_MEDIUM;
				} else if (!strcmp(value_name, "large")) {
					level.size = LEVEL_LARGE;
				}
			} else if (!strcmp(property_name, "max_cats_spawn")) {
				if (intval(value_name, &int_value)) {
					level.max_cats_spawn = int_value;
				}
			} else if (!strcmp(property_name, "movable_block_density")) {
				if (intval(value_name, &int_value)) {
					level.movable_block_density = int_value;
				}
			} else if (!strcmp(property_name, "stationary_block_density")) {
				if (intval(value_name, &int_value)) {
					level.stationary_block_density = int_value;
				}
			} else if (!strcmp(property_name, "trap_density")) {
				if (intval(value_name, &int_value)) {
					level.trap_density = int_value;
				}
			} else if (!strcmp(property_name, "sinkhole_density")) {
				if (intval(value_name, &int_value)) {
					level.sinkhole_density = int_value;
				}
			} else if (!strcmp(property_name, "yarn")) {
				if (!strcmp(value_name, "true")) {
					level.yarn = true;
				} else if (!strcmp(value_name, "false")) {
					level.yarn = false;
				}
			}
		} else if (!strcmp(line, "---\n")) {
			levels[num_levels] = level;
			num_levels++;

			level = DEFAULT_LEVEL_PROPERTIES;
		}
	}

	set_current_level(levels[0].id);

	fclose(fh);
}

void Level::load_map(const u8 data[LEVEL_HEIGHT][LEVEL_WIDTH]) {
	for (u8 i = 0; i < LEVEL_WIDTH; i++) {
		for (u8 j = 0; j < LEVEL_HEIGHT; j++) {
			switch (data[j][i]) {
				case LEVEL_TILE_EMPTY:
					map.set_tile(i, j, TILE_EMPTY);
					break;

				case LEVEL_TILE_MOVABLE:
					map.set_tile(i, j, TILE_MOVABLE_BLOCK);
					break;

				case LEVEL_TILE_STATIONARY:
					map.set_tile(i, j, TILE_STATIONARY_BLOCK);
					break;
			}
		}
	}
}

void Level::randomly_disperse_tile(LevelDensity density, TileNum tile) {
	for (u16 i = 0; i < density; i++) {
		u8 random_x;
		u8 random_y;

		random_x = rand() / (RAND_MAX / (LEVEL_WIDTH - 2)) + 1;
		random_y = rand() / (RAND_MAX / (LEVEL_HEIGHT - 2)) + 1;

		set_tile(random_x, random_y, tile);
	}
}

